<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">





  <link rel="alternate" href="/atom.xml" title="renhao" type="application/atom+xml">






<meta name="description" content="目前使用float浮动做了很多其本职工作之外的事，比如浮动方法实现分栏布局或者列表排列都可以用其他css属性实现，实现不了的很少，如文字环绕图片（这才是float真正的意义所在）。">
<meta property="og:type" content="article">
<meta property="og:title" content="浮动">
<meta property="og:url" content="https://huangrenhao.gitee.io/2019/09/13/css-float/index.html">
<meta property="og:site_name" content="renhao">
<meta property="og:description" content="目前使用float浮动做了很多其本职工作之外的事，比如浮动方法实现分栏布局或者列表排列都可以用其他css属性实现，实现不了的很少，如文字环绕图片（这才是float真正的意义所在）。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-03T08:50:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="浮动">
<meta name="twitter:description" content="目前使用float浮动做了很多其本职工作之外的事，比如浮动方法实现分栏布局或者列表排列都可以用其他css属性实现，实现不了的很少，如文字环绕图片（这才是float真正的意义所在）。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangrenhao.gitee.io/2019/09/13/css-float/">





  <title>浮动 | renhao</title>
  








<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">renhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangrenhao.gitee.io/2019/09/13/css-float/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="renhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="renhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">浮动</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-13T09:31:29+08:00">
                2019-09-13
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/css/" itemprop="url" rel="index">
                    <span itemprop="name">css</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/09/13/css-float/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/09/13/css-float/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>目前使用float浮动做了很多其本职工作之外的事，比如浮动方法实现分栏布局或者列表排列都可以用其他css属性实现，实现不了的很少，如文字环绕图片（这才是float真正的意义所在）。<a id="more"></a><a href="https://www.zhangxinxu.com/wordpress/2010/01/css-float%e6%b5%ae%e5%8a%a8%e7%9a%84%e6%b7%b1%e5%85%a5%e7%a0%94%e7%a9%b6%e3%80%81%e8%af%a6%e8%a7%a3%e5%8f%8a%e6%8b%93%e5%b1%95%e4%b8%80/" target="_blank" rel="noopener">张鑫旭原文</a></p>
<h3 id="包裹性"><a href="#包裹性" class="headerlink" title="包裹性"></a>包裹性</h3><p>浮动就是个带有方位的<code>display:inline-block</code>属性，<code>display:inline-block</code>在某种意义上的作用就是包裹（浮动也有类似效果）。例如实现宽度自适应按钮，宽度自适应的关键就是要让按钮的大小自适应与文字的宽度，这就需要按钮要自动包裹在文字的外面，可以使用<code>display:inline-block</code>或者<code>float</code>。故<code>float:left;display:block</code>很多情况下<code>display:block</code>都是多余的，但<code>float</code>无法等同于<code>display:inline-block</code>，主要原因就是浮动的方向性，<code>display:inline-block</code>仅仅是一个水平排列方向，从左往右，而<code>float</code>可以从左往右也可从右往左。</p>
<h3 id="破坏性"><a href="#破坏性" class="headerlink" title="破坏性"></a>破坏性</h3><p>float让文字环绕图片的原理：浮动破坏了正常的line boxes。在图片上加入float属性后图片的inline boxes就不存在了，图片就会从line boxes上脱离出来，跟随自身的方位属性靠边排列。图片的inline boxes不存在会使图片无法与文字同行显示并且<strong>没有了高度</strong>（无inline boxes-&gt;无line boxes-&gt;无高度）</p>
<h3 id="浮动与绝对定位"><a href="#浮动与绝对定位" class="headerlink" title="浮动与绝对定位"></a>浮动与绝对定位</h3><p>与浮动元素一样，绝对定位元素也具有包裹性，此包裹性适用于任何元素，浮动元素与绝对定位元素的差别在于：绝对定位的元素脱离了文档流，而浮动元素依旧在文档流中；而这造成的显示上的差异就是：同处于文档流中的文字实体不会与浮动元素重叠，而会与绝对定位元素重叠。这也会是文字环绕显示的重要原因之一，虽然图片实际占据的高度为0，但是由于其宽度实体存在（包裹性），同样是content area实体的文字不会与之重叠（外部容器盒子containing box如<code>&lt;p&gt;、&lt;div&gt;、&lt;li&gt;</code>会重叠）。</p>
<h3 id="浮动实现li（block元素）水平排列"><a href="#浮动实现li（block元素）水平排列" class="headerlink" title="浮动实现li（block元素）水平排列"></a>浮动实现li（block元素）水平排列</h3><p>在li元素之上添加float属性后，比如说<code>float:left</code>，包裹性使得li宽度紧贴内部元素，包裹性让元素变成类似于inline box的元素，破坏性破坏inline box元素，这两者并不冲突（先后顺序）。破坏了inline box，没有了高度，故其后面还有相同的li标签就会水平排列，如果li标签并没有float此时li标签还是会与float属性的li标签同一水平线排列（浮动破坏了实际高度，只是图片是个实体，无法与同样实体的同一文档流的图片重合）。</p>
<h3 id="浮动造成的高度塌陷"><a href="#浮动造成的高度塌陷" class="headerlink" title="浮动造成的高度塌陷"></a>浮动造成的高度塌陷</h3><p>高度塌陷也是证明页面布局不是浮动的本职工作，破坏了inline boxes后没有了高度，同样会使父标签高度缺失。</p>
<p>浮动用来做元素环绕不会造成高度塌陷：元素（比如文字）其自身是含有inline boxes高度的，这是inline水平的元素形成高度的基础，虽然浮动元素没有高度，但是其周围环绕的元素是有高度的，只要环绕元素比浮动元素高度高，父标签无高度的问题自然就没有了。</p>
<p>解决高度塌陷：清除浮动</p>
<p>IE下清除浮动：使元素haslayout，宽度值、高度值、绝对定位、zoon、浮动都可以让元素haslayout。</p>
<ul>
<li><p>直接一个<code>&lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;</code>当做最后一个子标签放到父标签（不推荐，浪费）</p>
<blockquote>
<p>clear 值可以取 left、right、both，分别表示某个方向上不允许出现浮动元素，none（默认值）、inherit 表示允许和从父元素继承</p>
</blockquote>
</li>
<li><p><code>overflow:hidden;zoom:1</code>，局限性在于可能会造成裁剪。</p>
</li>
<li><p>伪元素after（父元素标签的最后一个子元素），在其中添加<code>clear:both</code>，一般还需要将其转化为 块级元素，综合起来最好。</p>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/12/css-centerandmiddle/" rel="next" title="垂直水平居中">
                <i class="fa fa-chevron-left"></i> 垂直水平居中
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/14/css-text-align/" rel="prev" title="display:inline-block与列表布局">
                display:inline-block与列表布局 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">renhao</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#包裹性"><span class="nav-number">1.</span> <span class="nav-text">包裹性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#破坏性"><span class="nav-number">2.</span> <span class="nav-text">破坏性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浮动与绝对定位"><span class="nav-number">3.</span> <span class="nav-text">浮动与绝对定位</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浮动实现li（block元素）水平排列"><span class="nav-number">4.</span> <span class="nav-text">浮动实现li（block元素）水平排列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浮动造成的高度塌陷"><span class="nav-number">5.</span> <span class="nav-text">浮动造成的高度塌陷</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">renhao</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">265.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '2P1xqUWiCoNm14MH4yhURlJi-gzGzoHsz',
        appKey: 'uJN0uagHIVgXtBO1OuLV9Ban',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
